<?php

    /**
     * @package      Modules
     * @subpackage   Base_Classes_IO
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Base_Classes_IO_FileIterator
    {

    ////////////////////////////////////////////////////////////////////////////
    //                                 Variables                              //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Base directories
         *
         * @var     array
         */
        private $_baseDir = array();

        /**
         * Full path
         *
         * @var     boolean
         */
        private $_fullpath = false;

        /**
         * Files list
         *
         * @var     array
         */
        private $_filesList = array();

    ////////////////////////////////////////////////////////////////////////////
    //                                 Methods                                //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Constructor
         *
         * @param       string          $baseDir
         * @param		boolean			$fullpath : should the files be named with fullpath
         */
        public function __construct($baseDir, $fullpath = false)
        {
            if (is_array($baseDir)) {
                foreach ($baseDir as $dir) {
                    if (!Core_IO_Directory::exists($dir)) {
                        throw new Core_Exception('Fatal error : '. $dir .' is not a valid directory');
                    }
                }
                
                $this->_baseDir     = $baseDir;
                
            } else {
                if (!Core_IO_Directory::exists($baseDir)) {
                    throw new Core_Exception('Fatal error : '. $baseDir .' is not a valid directory');
                }
                
                $this->_baseDir[]     = $baseDir;
            }

            $this->_fullpath    = $fullpath;
        }

        /**
         * Iterates
         *
         * @return      array
         */
        public function iterate()
        {
            $this->_filesList = array();

            foreach ($this->_baseDir as $baseDir) {
                $this->_iterate($this->_baseDir . DIRECTORY_SEPARATOR);
            }

            return $this->_filesList;
        }

        /**
         * Iterates recursively
         *
         * @param       string      $dir
         */
        private function _iterate($dir)
        {
            foreach (Core_IO_Directory::getFiles($dir, true) as $f) {
                $name = str_replace($dir, '', $f);
                $path = ($this->_fullpath) ? $f : $name;

                $this->_filesList[] = array(
                                             'path' => $path,
                                             'name' => $name
                                           );
            }

            foreach (Core_IO_Directory::getDirectories($dir, true) as $subDir) {
                if (!strpos($subDir, ".svn")) {
                    $this->_iterate($subDir . DIRECTORY_SEPARATOR);
                }
            }
        }
    }